package cn.geekyspace.scanhive.domain.mapper;


import cn.geekyspace.scanhive.domain.entity.OperatorUserHistory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface OperatorUserHistoryMapper extends BaseMapper<OperatorUserHistory> {


    /**
     * 插入或更新用户历史记录（单条 SQL）
     */
    @Insert("""
        INSERT INTO operator_user_history (username, last_access_time)
        VALUES (#{username}, NOW())
        ON DUPLICATE KEY UPDATE last_access_time = NOW()
        """)
    void insertOrUpdate(@Param("username") String username);

    /**
     * 批量插入或更新
     */
    @Insert({
            "<script>",
            "INSERT INTO operator_user_history (username, last_access_time) VALUES ",
            "<foreach collection='usernames' item='username' separator=','>",
            "(#{username}, NOW())",
            "</foreach>",
            "ON DUPLICATE KEY UPDATE last_access_time = NOW()",
            "</script>"
    })
    void insertOrUpdateBatch(@Param("usernames") List<String> usernames);

}
